#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

run_code_quality_checks() {
  bundle exec rubocop .
}

run_unit_tests() {
  bundle exec rake test:coverage
}

run_integration_tests() {
  local pwd=$PWD
  local root="$pwd/test/isolation"

  for test in $(find $root -name '*_test.rb')
  do
    run_test $test

    if [ $? -ne 0 ]; then
      local exit_code=$?
      echo "Failing test: $test"
      exit $exit_code
    fi
  done
}

run_test() {
  local test=$1

  printf "\n\n\nRunning: $test\n"
  ruby -Itest $test
}

main() {
  run_code_quality_checks &&
    run_unit_tests &&
    run_integration_tests
}

main
